/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doanjava;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.text.Document;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.w3c.dom.xpath.XPathResult;

import org.jsoup.*;
/**
 *
 * @author Mr_Quay
 */
public class Xpath {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, XPatherException {
        //Duyet theo tin thoi su của vnexpress.net
        //String ulrweb = "http://vnexpress.net/tin-tuc/thoi-su";
        String ulrweb = "http://hcm.24h.com.vn/thoi-trang-c78.html";
        //String xpathtitle = "//div[@class='folder-news']/div[@class='right-fnews']/h2[@class='h2Title-14']";
        String xpathtitle = "/html/body/div[6]";
        String xpathcontent = "/html/body/div[6]";

        HtmlCleaner cleaner = new HtmlCleaner();
        CleanerProperties props = cleaner.getProperties();
        props.setAllowHtmlInsideAttributes(true);
        props.setAllowMultiWordAttributes(true);

        props.setRecognizeUnicodeChars(true);
        props.setOmitComments(true);

        URL url = new URL(ulrweb);
        URLConnection conn = url.openConnection();
        TagNode node = cleaner.clean(new InputStreamReader(conn.getInputStream()));
        Object[] title = node.evaluateXPath(xpathtitle);
        Object[] content = node.evaluateXPath(xpathcontent);

        //System.out.print(node);

        if (title.length > 0) {
            for (int i = 0; i < title.length; i++) {
                System.out.println("STT: " + (i + 1));
                TagNode info_title = (TagNode) title[i];
                System.out.println("Tiêu đề: " + info_title.getText().toString().trim().replace("&nbsp;", ""));
                TagNode info_content = (TagNode) content[i];
                System.out.println("Nội dung: " + info_content.getText().toString().trim());
                System.out.print("\n");
            }
        }
    }
}
